package edu.iss.javaproject.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import org.springframework.web.bind.annotation.SessionAttributes;

import edu.iss.javaproject.exception.LeaveNotFoundException;
import edu.iss.javaproject.exception.UserNotFoundException;
import edu.iss.javaproject.model.LeaveRecord;
import edu.iss.javaproject.model.Users;
import edu.iss.javaproject.service.LAPService;

@Controller

@RequestMapping(value="/staffHome")
@SessionAttributes("sessionuser")
public class ManagerController {

	@Autowired
	private LAPService userService;


	@RequestMapping(value = "/leavelist2", method = RequestMethod.GET)
	public String getLeaveList(@ModelAttribute("sessionuser") Users user,Model model) {
		Users manager= user;
		System.out.println(manager.toString());
		List<LeaveRecord> leaverecords = userService.findAllLeaveRecordsByManagerID( manager.getId());
		model.addAttribute("leaveRecords", leaverecords);
		model.addAttribute("sessionuser", manager);
		System.out.println(manager.toString());
		return "LeaveList2";
	}

	@RequestMapping(value = "LeaveDetail/{refNo}", method = RequestMethod.GET)
	public String GetleaveDetail(@PathVariable("refNo") int Id, Model model,@ModelAttribute("sessionuser") Users u) throws UserNotFoundException {

		System.out.println(u.toString());
		try {
			LeaveRecord l_leaveRecord = userService.findByrefNO(Id);
			Users user = userService.findByID(l_leaveRecord.getUsers().getId());
					System.out.println(l_leaveRecord.getUsers().getId());;
			model.addAttribute("userforleaverecord",user);
			model.addAttribute("leavedetail", l_leaveRecord);
		} catch (LeaveNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "LeaveDetail";
	}
	
	@RequestMapping(value = "LeaveDetail/{refNo}", method = RequestMethod.POST)
	public String UpdaeleaveDetail(@ModelAttribute("sessionuser") Users user,@ModelAttribute("leavedetail") LeaveRecord record) throws LeaveNotFoundException {
		
		//Users manager = user;
		String aa = user.getId().toString();
		record.setApprovedBy(user.getId().toString());
		System.out.println("USer ID "+user.toString());
		System.out.println(record.toString());
		userService.updateLeave(record);
	
		
		return "redirect:/staffHome/leavelist2";
	}
	
	
}
